package tangweijie.clock;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.LinearLayout;

public class ClockTestActivity extends Activity {
	
	protected static final int MSG_CLOCK = 0x1234;  
	private LinearLayout pannel; 
	private DrawClock clock;
	private Thread clockTestThread;
	public  Handler myHandler;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        pannel=(LinearLayout)findViewById(R.id.clock);
        clock=new DrawClock(this);
        pannel.addView(clock);
        myHandler=new Handler(){
        	public void handleMessage(Message msg){
        		if(msg.what==MSG_CLOCK){
        			pannel.removeView(clock);
        			clock=new DrawClock(ClockTestActivity.this);
        			pannel.addView(clock);
        		}
        		super.handleMessage(msg);
        	}
        };
        
        clockTestThread=new Loop();
        clockTestThread.start();
    }
    
    class Loop extends Thread{
    	public void run(){
    		super.run();
    		try{
    			do{
    			System.out.println("runtimes");
    			Thread.sleep(1000);
    			Message m=new Message();
    			m.what=MSG_CLOCK;
    			ClockTestActivity.this.myHandler.sendMessage(m);
    			}while(ClockTestActivity.Loop.interrupted()==false);
    		}catch(Exception e){
    			System.out.println("Exception Catched");
    			e.printStackTrace();
    		}
    	}
    }
}